package com.lottery.im.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lottery.im.R;

/**
 * Created by lianwufeng on 2017/10/9.
 * 群公告 对话框
 */

public class GroupNotifDialogBuilder extends Dialog {

  private static Context tmpContext;
  private static GroupNotifDialogBuilder instance;

  private View mDialogView;
  private RelativeLayout mLayoutAll;

  private TextView mTvTitle;
  private TextView mTvContent;

  public GroupNotifDialogBuilder(Context context) {
    super(context);
    init(context);
  }

  public GroupNotifDialogBuilder(Context context, int theme) {
    super(context, theme);
    init(context);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WindowManager.LayoutParams params = getWindow().getAttributes();
    //params.height = ViewGroup.LayoutParams.MATCH_PARENT;
    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
    getWindow().setAttributes(params);
    setCancelable(true);
    setCanceledOnTouchOutside(true);
  }

  public static GroupNotifDialogBuilder getInstance(Context context) {

    if (instance == null || !tmpContext.equals(context)) {
      synchronized (GroupNotifDialogBuilder.class) {
        if (instance == null || !tmpContext.equals(context)) {
          instance = new GroupNotifDialogBuilder(context, R.style.dialog_untran);
        }
      }
    }
    tmpContext = context;
    return instance;
  }

  private void init(Context context) {

    mDialogView = View.inflate(context, R.layout.dialog_group_notiy, null);
    mLayoutAll = mDialogView.findViewById(R.id.layout_all);

    setContentView(mDialogView);

    //this.setOnShowListener(new OnShowListener() {
    //  @Override
    //  public void onShow(DialogInterface dialogInterface) {
    //    BaseEffects animator = new ZoomEffects();
    //    animator.start(mLayoutAll);
    //  }
    //});

    mLayoutAll.setOnClickListener(view -> dismiss());

    mDialogView.findViewById(R.id.layout_dialog).setOnClickListener(view -> {
    });

    mTvTitle = mDialogView.findViewById(R.id.tv_title);
    mTvContent = mDialogView.findViewById(R.id.tv_content);
    findViewById(R.id.btn_confirm).setOnClickListener(view -> dismiss());
  }

  public GroupNotifDialogBuilder setTitle(String title) {
    if (TextUtils.isEmpty(title)) {
      mTvTitle.setVisibility(View.GONE);
    } else {
      mTvTitle.setText(title);
    }
    return this;
  }

  public GroupNotifDialogBuilder setContent(String content) {
    mTvContent.setText(content);
    return this;
  }

  @Override public void dismiss() {
    super.dismiss();
    instance = null;
    tmpContext = null;
  }

  @Override
  public void show() {
    super.show();
  }
}
